package com.fr.cell.core.date;

import com.fr.base.FRContext;
import com.fr.cell.core.layout.LayoutFactory;
import com.sun.java.swing.plaf.motif.MotifComboBoxUI;
import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;
import java.awt.Cursor;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxEditor;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.plaf.metal.MetalComboBoxUI;

public class JDatePicker extends JComboBox
  implements Serializable
{
  public static final int STYLE_CN_DATE = 0;
  public static final int STYLE_CN_DATE1 = 1;
  public static final int STYLE_CN_DATETIME = 2;
  public static final int STYLE_CN_DATETIME1 = 3;
  private int formatStyle;
  private SimpleDateFormat dateFormat;
  private SingleObjectComboBoxModel model;
  JDateDocument dateDocument;

  public JDatePicker()
    throws UnsupportedOperationException
  {
    this(3);
  }

  public JDatePicker(int paramInt)
    throws UnsupportedOperationException
  {
    this(paramInt, new Date());
  }

  public JDatePicker(int paramInt, Date paramDate)
    throws UnsupportedOperationException
  {
    this.formatStyle = 2;
    this.dateFormat = null;
    this.model = new SingleObjectComboBoxModel();
    this.dateDocument = null;
    setStyle(paramInt);
    setEditable(true);
    setCursor(new Cursor(0));
    JTextField localJTextField = (JTextField)getEditor().getEditorComponent();
    localJTextField.setHorizontalAlignment(0);
    this.dateDocument = new JDateDocument(localJTextField, this.dateFormat);
    localJTextField.setDocument(this.dateDocument);
    setModel(this.model);
    setSelectedItem((paramDate == null) ? new Date() : paramDate);
  }

  public void setStyle(int paramInt)
    throws UnsupportedOperationException
  {
    this.formatStyle = paramInt;
    this.dateFormat = getDateFormat(paramInt);
    this.model.setDateFormat(this.dateFormat);
    if (this.dateDocument != null)
      this.dateDocument.setDateFormat(this.dateFormat);
  }

  private static SimpleDateFormat getDateFormat(int paramInt)
    throws UnsupportedOperationException
  {
    switch (paramInt)
    {
    case 0:
      return new SimpleDateFormat("yyyy/MM/dd");
    case 1:
      return new SimpleDateFormat("yyyy-MM-dd");
    case 2:
      return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    case 3:
      return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }
    throw new UnsupportedOperationException("invalid formatStyle parameter!");
  }

  public int getStyle()
  {
    return this.formatStyle;
  }

  public Date getSelectedDate()
    throws ParseException
  {
    return this.dateFormat.parse(getSelectedItem().toString());
  }

  public void setSelectedDate(Date paramDate)
    throws ParseException
  {
    setSelectedItem(this.dateFormat.format(paramDate));
  }

  public void setSelectedItem(Object paramObject)
  {
    this.model.setSelectedItem(paramObject);
    super.setSelectedItem(paramObject);
  }

  public void updateUI()
  {
    Object localObject = (ComboBoxUI)UIManager.getUI(this);
    if (localObject instanceof MetalComboBoxUI)
      localObject = new MetalDateComboBoxUI(this);
    else if (localObject instanceof MotifComboBoxUI)
      localObject = new MotifDateComboBoxUI(this);
    else
      localObject = new WindowsDateComboBoxUI(this);
    setUI((ComboBoxUI)localObject);
  }

  static SimpleDateFormat access$000(JDatePicker paramJDatePicker)
  {
    return paramJDatePicker.dateFormat;
  }

  class MotifDateComboBoxUI extends MotifComboBoxUI
  {
    private final JDatePicker this$0;

    MotifDateComboBoxUI()
    {
      this.this$0 = paramJDatePicker;
    }

    protected ComboPopup createPopup()
    {
      return new JDatePicker.DatePopup(this.this$0, this.comboBox);
    }
  }

  class WindowsDateComboBoxUI extends WindowsComboBoxUI
  {
    private final JDatePicker this$0;

    WindowsDateComboBoxUI()
    {
      this.this$0 = paramJDatePicker;
    }

    protected ComboPopup createPopup()
    {
      return new JDatePicker.DatePopup(this.this$0, this.comboBox);
    }
  }

  class MetalDateComboBoxUI extends MetalComboBoxUI
  {
    private final JDatePicker this$0;

    MetalDateComboBoxUI()
    {
      this.this$0 = paramJDatePicker;
    }

    protected ComboPopup createPopup()
    {
      return new JDatePicker.DatePopup(this.this$0, this.comboBox);
    }
  }

  class DatePopup extends BasicComboPopup
    implements ChangeListener
  {
    JCalendarPanel calendarPanel;
    private final JDatePicker this$0;

    public DatePopup(, JComboBox paramJComboBox)
    {
      super(paramJComboBox);
      this.this$0 = paramJDatePicker;
      this.calendarPanel = null;
      setLayout(LayoutFactory.createBorderLayout());
      this.calendarPanel = new JCalendarPanel();
      this.calendarPanel.addDateChangeListener(this);
      add(this.calendarPanel, "Center");
      setBorder(BorderFactory.createEmptyBorder());
    }

    protected void firePropertyChange(, Object paramObject1, Object paramObject2)
    {
      if (paramString.equals("visible"))
        if ((paramObject1.equals(Boolean.FALSE)) && (paramObject2.equals(Boolean.TRUE)))
          try
          {
            String str = this.comboBox.getSelectedItem().toString();
            Date localDate = JDatePicker.access$000(this.this$0).parse(str);
            this.calendarPanel.setSelectedDate(localDate);
          }
          catch (Exception localException)
          {
            FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
          }
        else if ((paramObject1.equals(Boolean.TRUE)) && (paramObject2.equals(Boolean.FALSE)));
      super.firePropertyChange(paramString, paramObject1, paramObject2);
    }

    public void stateChanged()
    {
      Date localDate = (Date)paramChangeEvent.getSource();
      String str = JDatePicker.access$000(this.this$0).format(localDate);
      if ((this.comboBox.isEditable()) && (this.comboBox.getEditor() != null))
        this.comboBox.configureEditor(this.comboBox.getEditor(), str);
      this.comboBox.setSelectedItem(str);
      this.comboBox.setPopupVisible(false);
    }
  }
}